package com.chuangjiangx.member.basic.ddd.domain.model;

import com.chuangjiangx.dddbase.Repository;
import com.chuangjiangx.domain.shared.model.Enable;
import com.chuangjiangx.domain.shared.model.Sex;
import com.chuangjiangx.member.common.utils.MbrStringUtils;
import com.chuangjiangx.member.basic.dao.mapper.InMemberMapper;
import com.chuangjiangx.member.basic.dao.model.InMember;
import com.chuangjiangx.member.basic.dao.model.InMemberExample;
import com.chuangjiangx.member.coupon.ddd.domain.model.SwicthEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 会员持久化类
 */
@Component
public class MemberRepository implements Repository<Member, MemberId> {

    @Autowired
    private InMemberMapper inMemberMapper;


    @Override
    public Member fromId(MemberId id) {
        InMember inMember = inMemberMapper.selectByPrimaryKey(id.getId());
        return wrap(inMember);
    }

    public Member fromMemberCardNum(String memberCardNum){
        InMemberExample example = new InMemberExample();
        example.createCriteria().andMemberCardNumEqualTo(memberCardNum);
        List<InMember> inMemberList = inMemberMapper.selectByExample(example);
        if(!inMemberList.isEmpty()){
            return wrap(inMemberList.get(0));
        }
        return null;
    }


    @Override
    public void update(Member member) {
        InMember inMember = unbox(member);
        inMember.setId(member.getId().getId());
//        inMember.setRegisterTime(member.getRegisterTime());
        inMember.setUpdateTime(new Date());
        inMemberMapper.updateByPrimaryKeySelective(inMember);
    }

    @Override
    public void save(Member member) {
        InMember inMember = unbox(member);
//        inMember.setRegisterTime(new Date());
        inMemberMapper.insertSelective(inMember);
        member.setId(new MemberId(inMember.getId()));
    }

    public long countByMerchantIdAndMobile(MerchantId merchantId, String mobile) {
        InMemberExample example = new InMemberExample();
        example.createCriteria()
                .andMerchantIdEqualTo(merchantId.getId())
                .andMobileEqualTo(mobile)
                .andEnableEqualTo(Enable.ENABLE.value);
        return inMemberMapper.countByExample(example);
    }

    /**
     * 根据商户id和手机号查询会员
     *
     * @param merchantId 商户id
     * @param mobile     手机号
     * @return 会员
     */
    public Member fromMerchantIdAndMobile(MerchantId merchantId, String mobile) {
        InMemberExample example = new InMemberExample();
        example.createCriteria()
                .andMerchantIdEqualTo(merchantId.getId())
                .andMobileEqualTo(mobile)
                .andEnableEqualTo(Enable.ENABLE.value);
        List<InMember> list = inMemberMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return wrap(list.get(0));
        }
        return null;
    }

    /**
     * 数据库对象转换成domain对象
     */
    private Member wrap(InMember inMember) {
        if (inMember == null) {
            return null;
        }
        OperationInfo registerOperationInfo = new OperationInfo(
                new MerchantId(inMember.getMerchantId()),
                inMember.getRegisterStoreId() == null ? null : new StoreId(inMember.getRegisterStoreId()),
                inMember.getRegisterStoreUserId() == null ? null : new StoreUserId(inMember.getRegisterStoreUserId()),
                inMember.getRegisterTerminalType() == null ? null : MbrSourceTerminal.getSourceTerminal(inMember.getRegisterTerminalType()),
                inMember.getRegisterTerminalNum() == null ? null : inMember.getRegisterTerminalNum(),
                inMember.getRegisterMerchantUserId() == null ? null : new MerchantUserId(inMember.getRegisterMerchantUserId()));

        return new Member(
                new MemberId(inMember.getId()), inMember.getMemberCardNum(),
                inMember.getNickName() == null ? null : inMember.getNickName(),
                inMember.getName(), inMember.getMobile(),
                Sex.getSex(inMember.getSex()),
                inMember.getBirthday(), inMember.getRegisterTime(),
                inMember.getUpdateTime(), Enable.getEnable(inMember.getEnable()),
                AgreementType.getAgreementType(inMember.getAgreement()),
                registerOperationInfo,inMember.getHeadimgurl(),
                inMember.getSyncWxStatus()==null?SwicthEnum.OFF:SwicthEnum.fromCode(inMember.getSyncWxStatus()),
                inMember.getSyncWxTime(), inMember.getFaceId()
        );
    }

    private InMember unbox(Member member) {
        InMember inMember = new InMember();
        inMember.setMemberCardNum(member.getMemberCardNum());
        inMember.setNickName(null == member.getNickName() ? null : member.getNickName());
        inMember.setName(MbrStringUtils.filterOffUtf8Mb4(member.getName()));
        inMember.setMobile(member.getMobile());
        inMember.setHeadimgurl(member.getHeadimgurl());
        inMember.setSex(null == member.getSex() ? null : member.getSex().value);
        inMember.setBirthday(member.getBirthday());
        inMember.setEnable(null == member.getEnable() ? null : member.getEnable().value);
        inMember.setAgreement(null == member.getAgreement() ? null : member.getAgreement().value);
        OperationInfo operationInfo = member.getOperationInfo();
        inMember.setMerchantId(operationInfo.getMerchantId().getId());
        inMember.setSyncWxStatus(member.getSyncWxStatus()==null?null:member.getSyncWxStatus().code);
        inMember.setSyncWxTime(member.getSyncWxTime());
        inMember.setFaceId(member.getFaceId());
        if (operationInfo.getStoreId() != null) {
            inMember.setRegisterStoreId(operationInfo.getStoreId().getId());
        }
        if (operationInfo.getStoreUserId() != null) {
            inMember.setRegisterStoreUserId(operationInfo.getStoreUserId().getId());
        }
        if (operationInfo.getTerminalType() != null) {
            inMember.setRegisterTerminalType(operationInfo.getTerminalType().value);
        }
        if (operationInfo.getTerminalNum() != null) {
            inMember.setRegisterTerminalNum(operationInfo.getTerminalNum());
        }
        if (operationInfo.getMerchantUserId() != null) {
            inMember.setRegisterMerchantUserId(operationInfo.getMerchantUserId().getId());
        }
        return inMember;
    }


    public void deleteById(Long id) {
        inMemberMapper.deleteByPrimaryKey(id);
    }


}
